﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using QuDao;
using FineUICore;
using Kalman;
using Kalman.Extensions;
using Newtonsoft.Json.Linq;
using System.Data;

namespace AdminUI.Pages.Finance
{
    /// <summary>
    /// 提现申请
    /// </summary>
    public partial class CashApplyModel : BaseModel
    {
        public void OnGet()
        {
            var req = new PagingRequest
            {
                PageIndex = 1,
                PageSize = 20
            };
            var result = CashApplyBO.Instance.Query(req, OnlineTID, DateTime.Now.AddYears(-1), DateTime.Now);
            ViewBag.DataSource = result.Data;
            ViewBag.Total = result.Total;
            ViewBag.PageSize = result.PageSize;

            ViewBag.Grid1SummaryData = GetSummaryData(result.Data);
        }

        private static JObject GetSummaryData(List<CashApply> list)
        {
            var summary = new JObject
            {
                { "UserID", "合计" },
                { "Amount", $"￥{list.Sum(p => p.Amount)}" }
            };

            return summary;
        }

        /// <summary>
        /// 执行会引起表格控件数据变化的相关操作
        /// </summary>
        /// <param name = "condition">搜索条件</param>
        /// <param name = "keyword">搜索关键字</param>
        /// <param name = "data">Grid控件查询数据对象</param>
        /// <returns></returns>
        public IActionResult OnPostGrid_DoAction(QueryData<long> data, DateTime beginDate, DateTime endDate, string condition, string keyword)
        {
            var grid = UIHelper.Grid("Grid1");
            var req = new PagingRequest
            {
                PageIndex = data.PageIndex + 1, //注意：数据表格组件分页索引从0开始
                PageSize = data.ChangedPageSize,
                SortExp = data.SortExp,
            };
            if (!string.IsNullOrEmpty(keyword) && !string.IsNullOrEmpty(condition))
            {
                req.FilterItems.Add(new FilterItem { Field = condition, Value = keyword });
            }

            if (data.IsChangedPageSize)
            {
                grid.PageSize(data.ChangedPageSize);
                grid.PageIndex(0); //改变PageSize大小后重置分页索引，表格控件页索引从0开始
                req.PageIndex = 1;
            }

            try
            {
                switch (data.ActionType)
                {
                    default:
                        break;
                }

                var result = CashApplyBO.Instance.Query(req, OnlineTID, beginDate, endDate);
                grid.RecordCount(result.Total);
                grid.DataSource(result.Data, data.Fields);
                grid.SummaryData(GetSummaryData(result.Data));
            }
            catch (Exception ex)
            {
                var msg = ex.Message;
                if (!(ex is AppException && ex.InnerException == null))
                {
                    msg = ex.ToString();
                }

                Alert.Show(msg, "错误信息", MessageBoxIcon.Error);
            }

            return UIHelper.Result();
        }
    }
}